package com.project.finance.system.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.project.finance.common.mapper.TbSysUserMapper;
import com.project.finance.common.po.TbSysUser;
import com.project.finance.common.vo.ResultMsg;
import com.project.finance.system.service.SystemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @Author: Brendan Li
 * @description 系统用户的业务实现
 * @Date: 2019/12/06/17:46
 */
@Service
public class SystemServiceImpl implements SystemService {
    @Autowired
    private TbSysUserMapper sysUserMapper;

    @Override
    public List<TbSysUser> listSysUsers() {
        List<TbSysUser> tbSysUsers = sysUserMapper.selectAll();
        return tbSysUsers;
    }

    @Override
    public List<TbSysUser> getSysUser(String loginName) {
        Example example = new Example(TbSysUser.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andLike("loginname","%"+loginName+"%");
        List<TbSysUser>  tbSysUser = sysUserMapper.selectByExample(example);

        return tbSysUser;
    }

    @Override
    public int saveSysUser(TbSysUser tbSysUser) {
        int insert = sysUserMapper.insert(tbSysUser);
        return insert;
    }

    @Override
    public int updateSysUser(TbSysUser tbSysUser) {
        return sysUserMapper.updateByPrimaryKey(tbSysUser);
    }

    @Override
    public int deleteSysUser(int id) {
        return sysUserMapper.deleteByPrimaryKey(id);
    }

    @Override
    public ResultMsg listSysUserByPage(int pageSize, int pageNum) {
        ResultMsg resultMsg = new ResultMsg();

        PageHelper.startPage(pageNum,pageSize);

        List<TbSysUser> tbSysUsers = sysUserMapper.selectAll();

        //获取结果集的分页信息
        PageInfo pageInfo = new PageInfo(tbSysUsers);
        //获取总记录数
        long total = pageInfo.getTotal();
        resultMsg.setTatol(total);
        resultMsg.setData(tbSysUsers);
        return resultMsg;
    }

    @Override
    public TbSysUser getSysUserByName(String loginname) {

        return sysUserMapper.selectByName(loginname);

    }
}
